package projectManagement;

import java.awt.Dimension;
import java.awt.Font;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class AddingMemberGUI extends JPanel implements ActionListener{

	DefaultListModel<String> allMemberListModel;
	DefaultListModel<String> addedMemberListModel;
	
	JList<String> allMemberList;
	JList<String> addedMemberList;
	
	ArrayList<String> allMember;
	ArrayList<String> addedMember;
	
	JFrame f;
	JButton btnOk;
	public AddingMemberGUI(ArrayList<String> allMember) {
		setPreferredSize(new Dimension(445, 420));
		setLayout(null);
		
		this.allMember = allMember;
		this.addedMember = new ArrayList<>();
		
		allMemberPanel();
		addedMemberPanel();
		
		JButton btnAdd = new JButton("ADD");
		btnAdd.setBounds(62, 366, 89, 23);
		btnAdd.addActionListener(this);
		btnAdd.putClientProperty("Quaqua.Button.style", "bevel");
		add(btnAdd);
		
		JButton btnDelete = new JButton("DELETE");
		btnDelete.setBounds(291, 366, 89, 23);
		btnDelete.putClientProperty("Quaqua.Button.style", "bevel");
		btnDelete.addActionListener(this);
		add(btnDelete);
		
		btnOk = new JButton("CANCEL");
		btnOk.setBounds(174, 355, 89, 65);
		btnOk.addActionListener(this);
		btnOk.putClientProperty("Quaqua.Button.style", "bevel");
		add(btnOk);
		
		
		f = new JFrame();
		f.getContentPane().add(this);
		f.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		f.setVisible(true);
		f.setResizable(false);
		f.pack();
		f.setLocationRelativeTo(null);
		

	}
	
	public void allMemberPanel(){
		JPanel allMemberPanel = new JPanel();
		allMemberPanel.setBorder(new TitledBorder(null, "All Member", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		allMemberPanel.setBounds(10, 11, 207, 344);
		add(allMemberPanel);
		allMemberPanel.setLayout(new BorderLayout(0, 0));
		
		allMemberListModel = new DefaultListModel<>();
		for(String x : allMember){
			allMemberListModel.addElement(x);
		}
		
		allMemberList = new JList<>();
		allMemberList.setFont(new Font("Tempus Sans ITC", Font.PLAIN, 15));
		allMemberList.setModel(allMemberListModel);
		allMemberList.setSelectedIndex(-1);
		allMemberList.setVisibleRowCount(0);
		allMemberList.setValueIsAdjusting(true);
		allMemberList.setLocation(0,0);
		allMemberList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		allMemberList.putClientProperty( "Quaqua.List.style", "striped");
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(allMemberList);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		allMemberPanel.add(scrollPane, BorderLayout.CENTER);
	}
	
	public void addedMemberPanel(){
		JPanel addedMemberPanel = new JPanel();
		addedMemberPanel.setBorder(new TitledBorder(null, "Added Member", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		addedMemberPanel.setBounds(227, 11, 207, 344);
		add(addedMemberPanel);
		addedMemberPanel.setLayout(new BorderLayout(0, 0));
		
		addedMemberListModel = new DefaultListModel<>();
		for(String x : addedMember){
			addedMemberListModel.addElement(x);
		}
		
		addedMemberList = new JList<>();
		addedMemberList.setFont(new Font("Tempus Sans ITC", Font.PLAIN, 15));
		addedMemberList.setModel(addedMemberListModel);
		addedMemberList.setSelectedIndex(-1);
		addedMemberList.setVisibleRowCount(0);
		addedMemberList.setValueIsAdjusting(true);
		addedMemberList.setLocation(0,0);
		addedMemberList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		addedMemberList.putClientProperty( "Quaqua.List.style", "striped");
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(addedMemberList);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		addedMemberPanel.add(scrollPane, BorderLayout.CENTER);
	}

	@Override
	public void actionPerformed(ActionEvent btn) {
		String cmd = btn.getActionCommand();
		
		if(cmd.equalsIgnoreCase("ADD")){
			if(!allMemberList.isSelectionEmpty()){
				addedMemberListModel.addElement(allMemberListModel.getElementAt(allMemberList.getSelectedIndex()));
				allMemberListModel.removeElementAt(allMemberList.getSelectedIndex());
			}else{
				JOptionPane.showMessageDialog(null, "Please Select a Member");
			}
			btnOk.setText("OK");
			
		}
		
		if(cmd.equalsIgnoreCase("DELETE")){
			if(!addedMemberList.isSelectionEmpty()){
				allMemberListModel.addElement(addedMemberListModel.get(addedMemberList.getSelectedIndex()));
				addedMemberListModel.remove(addedMemberList.getSelectedIndex());
				if(addedMemberListModel.size() == 0){
					btnOk.setText("CANCEL");
				}
			}else{
				JOptionPane.showMessageDialog(null, "Please Select a Member");
			}
			
		}
		
		if(cmd.equalsIgnoreCase("OK")){
			ArrayList<String> allMemberToAdd = new ArrayList<>();
			for(int i=0; i<addedMemberListModel.size(); i++){
				allMemberToAdd.add(addedMemberListModel.getElementAt(i));
			}
			ClientGUI.sendAddMemberToProject(allMemberToAdd);
			f.setVisible(false);
		}
		
		if(cmd.equalsIgnoreCase("CANCEL")){
			f.setVisible(false);
		}
		
	}
}
